

package com.latam.arq.example.web.managedbeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;


import com.latam.arq.commons.exceptions.LATAMException;
import com.latam.arq.example.crud.ejb.services.PamCRUDService;
import com.latam.arq.example.domain.PamBean;
 
@ManagedBean(name = "dfCarsView")
@RequestScoped
public class DFCarsView implements Serializable {
     
	
	@EJB
	private PamCRUDService pamEJBServices;
		
    private List<PamBean> cars;
 
     
    @PostConstruct
    public void init() {
		try{
			this.cars = pamEJBServices.list();
		}catch(final LATAMException e){
			this.cars = new ArrayList<PamBean>();
		}
		
		createDynamicColumns();
    }
     
    public List<PamBean> getCars() {
        return cars;
    }

    public void selectCarFromDialog(PamBean car) {
        RequestContext.getCurrentInstance().closeDialog(car);
    }
    
    private final static List<String> VALID_COLUMN_KEYS = Arrays.asList("id", "rut","name","lastName");
    
    private String columnTemplate = "id rut name";
     
    private List<ColumnModel> columns;
     
    private List<PamBean> filteredCars;
     



    public List<PamBean> getFilteredCars() {
        return filteredCars;
    }
 
    public void setFilteredCars(List<PamBean> filteredCars) {
        this.filteredCars = filteredCars;
    }
 

    public String getColumnTemplate() {
        return columnTemplate;
    }
 
    public void setColumnTemplate(String columnTemplate) {
        this.columnTemplate = columnTemplate;
    }
 
    public List<ColumnModel> getColumns() {
        return columns;
    }
 
    private void createDynamicColumns() {
        String[] columnKeys = columnTemplate.split(" ");
        columns = new ArrayList<ColumnModel>();  
         
        for(String columnKey : columnKeys) {
            String key = columnKey.trim();
             
            if(VALID_COLUMN_KEYS.contains(key)) {
                columns.add(new ColumnModel(columnKey.toUpperCase(), columnKey));
            }
            
            
        }
    }
     
    public void updateColumns() {
        //reset table state
        UIComponent table = FacesContext.getCurrentInstance().getViewRoot().findComponent(":form:cars");
        table.setValueExpression("sortBy", null);
         
        //update columns
        createDynamicColumns();
    }
     
    static public class ColumnModel implements Serializable {
 
        private String header;
        private String property;
 
        public ColumnModel(String header, String property) {
            this.header = header;
            this.property = property;
        }
 
        public String getHeader() {
            return header;
        }
 
        public String getProperty() {
            return property;
        }
    }
    
    
    
    
}
